package com.cq.androidtools.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.core.app.NotificationCompat
import com.cq.androidtools.R
import com.cq.androidtools.databinding.AcNotificationBinding
import com.cq.base.activity.BaseViewBindingActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 作者: CQ
 * 日期: 2022-07-07
 * 说明:
 */
class NotificationActivity : BaseViewBindingActivity<AcNotificationBinding>() {
    override fun initData() {
        super.initData()
        mBinding.text.setOnClickListener {
            createNotificationForNormal()
        }
        mBinding.text2.setOnClickListener {
            createNotificationForHigh()
        }
        mBinding.text3.setOnClickListener {
            createNotificationForProgress()
        }
    }

    val mProgressChannelId = "channel_id"
    val mProgressChannelName = "channel_name"
    val NOTIFY_ID = 1
    val mManager by lazy { getSystemService(NOTIFICATION_SERVICE) as NotificationManager }
    private fun createNotificationForNormal() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(mProgressChannelId, mProgressChannelName, NotificationManager.IMPORTANCE_LOW).apply {
                description = "miaoshu"
                setShowBadge(false) // 是否在桌面显示角标
            }

            mManager.createNotificationChannel(channel)

        }
        val intent = Intent(this, NotificationActivity2::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        val mBuilder = NotificationCompat.Builder(this, mProgressChannelId)
            .setSmallIcon(R.drawable.scan_normal)
            .setContentTitle("title")
            .setContentText("text")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.scan_normal))
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setColor(Color.parseColor("#3DDC84"))
        mManager.notify(NOTIFY_ID, mBuilder.build())

    }

    private fun createNotificationForHigh() {
        val intent = Intent(this, NotificationActivity2::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(mProgressChannelId, mProgressChannelName, NotificationManager.IMPORTANCE_HIGH)
            channel.setShowBadge(true)
            mManager.createNotificationChannel(channel)
        }
        val mBuilder = NotificationCompat.Builder(this@NotificationActivity, mProgressChannelId)
            .setContentTitle("重要通知")
            .setContentText("重要通知内容")
            .setSmallIcon(R.drawable.scan_normal)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.scan_normal))
            .setAutoCancel(true)
            .setColor(Color.parseColor("#3DDC84"))
            .setNumber(999) // 自定义桌面通知数量
            .addAction(R.drawable.scan_normal, "去看看", pendingIntent) // 通知上的操作
            .setCategory(NotificationCompat.CATEGORY_MESSAGE) // 通知类别，"勿扰模式"时系统会决定要不要显示你的通知
            .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) // 屏幕可见性，锁屏时，显示icon和标题，内容隐藏
        mManager.notify(2, mBuilder.build())
    }

    private var mProgress = 0

    private fun createNotificationForProgress() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(mProgressChannelId, mProgressChannelName, NotificationManager.IMPORTANCE_DEFAULT)
            mManager.createNotificationChannel(channel)
        }
        val progressMax = 100
        val mBuilder = NotificationCompat.Builder(this@NotificationActivity, mProgressChannelId)
            .setContentTitle("进度通知")
            .setContentText("下载中：$mProgress%")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.scan_normal))
            // 第3个参数indeterminate，false表示确定的进度，比如100，true表示不确定的进度，会一直显示进度动画，直到更新状态下载完成，或删除通知
            .setProgress(progressMax, mProgress, false)
        mManager.notify(3, mBuilder.build())

        CoroutineScope(Dispatchers.IO).launch {
            while (mProgress < 100) {
                mProgress += 10
                delay(500)
                mBuilder.setContentText("下载中：$mProgress%").setProgress(progressMax, mProgress, false)
                mManager.notify(3, mBuilder.build())
            }
            mBuilder.setContentTitle("下载完成")
                .setContentText("")
                .setProgress(0, 0, false)
            mManager.notify(3, mBuilder.build())
        }
    }

}